<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsCustomerBean {
	
	public function __construct() {
	// contructor function
	}
	
	/**
     * Save customer information to database.
     * Customer information is POST values
     * field name is defined in input form
     * See smarty template for detail of name of form's element.
     */
	
	
	public function saveCustomer($db, $info) {
    	$sql=sprintf(
					"INSERT INTO %s(customer_gender,customer_firstname,customer_lastname,customer_dob,customer_company,".
					"delivery_gender,delivery_firstname,delivery_lastname,delivery_street_address,delivery_postcode,delivery_city,delivery_state,delivery_country,".
					"billing_gender,billing_firstname,billing_lastname,billing_street_address,billing_postcode,billing_city,billing_state,billing_country,".
					"customer_email,customer_telephone,customer_cellphone,customer_fax,customer_password,customer_newsletter,customer_registered) " .
					"VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',now())",
    				TBL_CUSTOMER, 
    				$info['customer_gender'],
    				$info['customer_firstname'],
    				$info['customer_lastname'],
					$info['customer_dob'],
					$info['customer_company'],
					$info['delivery_gender'],
					$info['delivery_firstname'],
    				$info['delivery_lastname'],
					$info['delivery_street_address'],
					$info['delivery_postcode'],
					$info['delivery_city'],
					$info['delivery_state'],
					$info['delivery_country_id'],
					$info['billing_gender'],
					$info['billing_firstname'],
    				$info['billing_lastname'],
					$info['billing_street_address'],
					$info['billing_postcode'],
					$info['billing_city'],
					$info['billing_state'],
					$info['billing_country_id'],
					$info['customer_email'],
					$info['customer_telephone'],
					$info['customer_cellphone'],
					$info['customer_fax'],
					crypt($info['customer_password'], '$ntp$'),
    				$info['customer_newsletter']);
		  
        		
    	$res = $db->query($sql);
		$id = -1;
		if($res == true) {	
			$id=$db->insert_id;
			return $id;
		}	
    	else
    		return $id;
    }
	
	
	public static function updateCustomer($db, $info) {
    	
    	$sql = sprintf("UPDATE %s SET " .
    			"customer_gender = '%s', " .
    			"customer_firstname = '%s', " .
    			"customer_lastname = '%s', " .
    			"customer_dob = '%s', " .
				"delivery_gender = '%s', " .
    			"delivery_firstname = '%s', " .
    			"delivery_lastname = '%s', " .
				"delivery_street_address = '%s', " .
    			"delivery_postcode = '%s', " .
    			"delivery_city = '%s', " .
    			"delivery_state = '%s', " .
    			"delivery_country = '%s', " .
				"billing_gender = '%s', " .
    			"billing_firstname = '%s', " .
    			"billing_lastname = '%s', " .
				"billing_street_address = '%s', " .
    			"billing_postcode = '%s', " .
    			"billing_city = '%s', " .
    			"billing_state = '%s', " .
    			"billing_country = '%s', " .
    			"customer_company = '%s', " .
    			"customer_email = '%s', " .
    			"customer_telephone = '%s', ".
				"customer_cellphone = '%s', ".
    			"customer_fax = '%s', " .
    			"customer_password = '%s', " .
    			"customer_newsletter = '%s' ," .
    			"customer_registered = now() " .
    			"WHERE  customer_id =" . $info['customer_id'],
    			TBL_CUSTOMER,
    			$info['customer_gender'],
    			$info['customer_firstname'],
    			$info['customer_lastname'],
    			$info['customer_dob'],
				$info['delivery_gender'],
    			$info['delivery_firstname'],
    			$info['delivery_lastname'],
				$info['delivery_street_address'],
    			$info['delivery_postcode'],
    			$info['delivery_city'],
    			$info['delivery_state'],
    			$info['delivery_country_id'],
				$info['billing_gender'],
    			$info['billing_firstname'],
    			$info['billing_lastname'],
				$info['billing_street_address'],
    			$info['billing_postcode'],
    			$info['billing_city'],
    			$info['billing_state'],
    			$info['billing_country_id'],
    			$info['customer_company'],
    			$info['customer_email'],
    			$info['customer_telephone'],
				$info['customer_cellphone'],
    			$info['customer_fax'],
    			$info['customer_password'],
    			$info['customer_newsletter']);
    	$res = $db->query($sql);

    	return $res;
    }
	
	public function updateCustomerShipping($db, $info){
    	
    	$sql = sprintf("UPDATE %s SET " .
    			"delivery_gender = '%s', " .
    			"delivery_firstname = '%s', " .
    			"delivery_lastname = '%s', " .
				"delivery_street_address = '%s', " .
    			"delivery_postcode = '%s', " .
    			"delivery_city = '%s', " .
    			"delivery_state = '%s', " .
    			"delivery_country = '%s', " .
    			"delivery_company = '%s', " .
    			"delivery_email_address = '%s', " .
    			"delivery_telephone = '%s' ".
    			"WHERE  customer_id =" . $_SESSION['cust_id'],
    			TBL_CUSTOMER,
    			$info['delivery_gender'],
    			$info['delivery_firstname'],
    			$info['delivery_lastname'],
				$info['delivery_street_address'],
    			$info['delivery_postcode'],
    			$info['delivery_city'],
    			$info['delivery_state'],
    			$info['delivery_country'],
    			$info['delivery_company'],
    			$info['delivery_email_address'],
    			$info['delivery_telephone']);
    		
    	$res = $db->query($sql);

    	return $res;
    }
	
	public function updateCustomerBilling($db, $info){
    	
    	$sql = sprintf("UPDATE %s SET " .
    			"billing_gender = '%s', " .
    			"billing_firstname = '%s', " .
    			"billing_lastname = '%s', " .
				"billing_street_address = '%s', " .
    			"billing_postcode = '%s', " .
    			"billing_city = '%s', " .
    			"billing_state = '%s', " .
    			"billing_country = '%s', " .
    			"billing_company = '%s', " .
    			"billing_email_address = '%s', " .
    			"billing_telephone = '%s' ".
    			"WHERE  customer_id =" . $_SESSION['cust_id'],
    			TBL_CUSTOMER,
    			$info['billing_gender'],
    			$info['billing_firstname'],
    			$info['billing_lastname'],
				$info['billing_street_address'],
    			$info['billing_postcode'],
    			$info['billing_city'],
    			$info['billing_state'],
    			$info['billing_country'],
    			$info['billing_company'],
    			$info['billing_email_address'],
    			$info['billing_telephone']);
    		
    	$res = $db->query($sql);

    	return $res;
    }
	
	public function activeAccount($db, $id)
    {
		$sql = sprintf("UPDATE %s SET " .
    			"customer_active = '1' " .
    			"WHERE customer_id =" . $id,
    			TBL_CUSTOMER);
		$res = $db->query($sql);		
	}
	
	public function getActiveCustomerById($db, $id) {
    	$sql = sprintf("SELECT c.customer_active FROM ".TBL_CUSTOMER." c ".
						" WHERE c.customer_id='$id'");
    	return $db->get_row($sql);
    }
	 /**
     * Get a customer by customer name
     * @param $db is a ezSQL_mysql object
     * @param $customer_name the customer_name want to get other information
     * @return The customer object. 
     * customer object contain fields is same as defined in database  
     */
    public function getCustomerByEmail($db, $customer_email) {
    	$sql = sprintf("SELECT * FROM ".TBL_CUSTOMER." WHERE customer_email='$customer_email' AND customer_active ='1'");
    	return $db->get_row($sql);
    }
	
	public function ExistsByEmail($db, $customer_email) {
    	$sql = sprintf("SELECT customer_id FROM ".TBL_CUSTOMER." WHERE customer_email='$customer_email'");
    	return $db->get_row($sql);
    }
	
	public function updatePassword($db, $email, $pass)
    {
    	$sql = sprintf("UPDATE %s SET " .
    					"customer_password = '%s' " .
    					"WHERE customer_email = '%s'", 
    					TBL_CUSTOMER,
    					mysql_real_escape_string($pass),
    					mysql_real_escape_string($email));		
    	$res = $db->query($sql);
		return $res;
    }
	
	public function getCustomerById($db, $id) {
    	$sql = sprintf("SELECT *,(SELECT t.country_name FROM ".TBL_COUNTRY." t WHERE c.customer_country_id = t.country_id) as customer_country_name, ".
						"(SELECT d.country_name FROM ".TBL_COUNTRY." d WHERE c.delivery_country = d.country_id) as delivery_country_name, ".
						"(SELECT b.country_name FROM ".TBL_COUNTRY." b WHERE c.billing_country = b.country_id) as billing_country_name ".
						"FROM ".TBL_CUSTOMER." c ".
						" WHERE c.customer_id='$id'");
    	$res =  $db->get_row($sql);
		return $res;
    }
	
	
	
	public function getAllEmail($db)
    {
    	$sql = sprintf("SELECT customer_email FROM %s", TBL_CUSTOMER);
    	return $db->get_results($sql);
    }
	
	public function getCustomerEmail($db, $id)
    {
    	$sql = sprintf("SELECT customer_email FROM " . TBL_CUSTOMER . " WHERE customer_id = $id");
    	$customeremail = $db->get_row($sql);
    	return $customeremail->customer_email;
    }
	
	 public function getCustomerPassword($db, $id)
    {
    	$sql = sprintf("SELECT customer_password FROM " . TBL_CUSTOMER . " WHERE customer_id = $id");
    	$pass = $db->get_row($sql);
    	return $pass->customer_password;
    }
	
	
	
	
}
?>
